$(function() {
	get_user_mess_and_token();
	if(user_id==0){
		location.href="http://127.0.0.1:8899/html/car_login.html?car_my_collection";
		return;
	}
	//切换查看视频收藏和咨询收藏的点击事件
	getClick();
	//回到顶部
	GotoTop();
	//登录
	$(".to_login").attr("href",
		"http://127.0.0.1:8899/html/car_login.html?car_my_collection/" + user_id);
});

/**
 * 根据用户登入id查询咨询收藏集合
 */
function getColltion_informationList() {
	$.post("http://127.0.0.1:8899/api/consumer/collection_informationController/select_collection_informationList_byId/" +
		user_id,
		function(data) {
			var str = "";
			var collection_informationDate = "";
			var informationcount = 0;
			$(data).each(function(index, item) {
				var date = new Date();
				var thisDate = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();

				var newDateCha = parseInt((new Date(thisDate) - new Date(item.information
					.information_release_date)) / (1000 * 60 * 60 * 24));
				if (newDateCha <= 7) {
					collection_informationDate = newDateCha + "天前";
				} else {
					collection_informationDate = item.information.information_release_date;
				}
				if (item.information.information_read_count / 10000 >= 1) {
					informationcount = (item.information.information_read_count / 10000).toFixed(2) + "万";
				} else {
					informationcount = item.information.information_read_count;
				}
				str += "<div class='jsx-1870675467'>" +
					"<div class='common-card_wrapper__Inr_n '>" +
					"<div>" +
					"<div class='tw-relative tw-text-common-black' style='font-size: 0px;'>" +
					"<a title='" + item.information.information_title +
					"' href='http://127.0.0.1:8899/html/car_information_details.html?" + item.information
					.information_id + "/0' target='_blank' class='jsx-1095092229' rel='opener'>" +
					"<div class='tw-rounded-4 g-load-img-wrap' style='display: block; overflow: hidden; position: relative; z-index: 0;'>" +
					"<div style='display: block; box-sizing: border-box; padding-top: 56%;'></div>" +
					"<img src='http://120.48.47.144:8899/images/" + item.information.information_images
					.information_images_1 + "' alt='" + item.information.information_title +
					"' width='250' height='140' class='zoom' style='position: absolute; inset: 0px; box-sizing: border-box; padding: 0px; border: none; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: cover;'>" +
					"</div></a>" +
					"<div class='jsx-1904556275 author-avatar_avatar-wrap__1pWQN'>" +
					"<div class='jsx-2217341492 avatar_avatar-img__170Bh tw-text-0'>" +
					"<a href='http://127.0.0.1:8899/html/car_my_homepage.html?" + item.cover_infor_user_id +
					"' title='" + item.cover_infor_user_name +
					"个人主页 ' target='_blank' rel='opener'>" +
					"<div class='avatar tw-rounded-full' style='display: inline-block; max-width: 100%; overflow: hidden; position: relative; z-index: 0;'>" +
					"<div style='box-sizing: border-box; display: block; max-width: 100%;'>" +
					"<img alt='' src='data:image/svg+xml;utf-8,<svg width=&quot;40&quot; height=&quot;40&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;/>' aria-hidden='true' role='presentation' style='max-width: 100%; display: block; margin: 0px; border: none; padding: 0px;'>" +
					"</div>" +
					"<img src='http://120.48.47.144:8899/images/" + item.cover_infor_head_portrait_url +
					"' alt='" + item.cover_infor_user_name +
					"头像' width='40' height='40' class='' style='position: absolute; inset: 0px; box-sizing: border-box; padding: 0px; border: none; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: cover;'>" +
					"</div><i class='jsx-2217341492 avatar_author-v__2Xocb author-v v-yellow'></i>" +
					"</a>" +
					"</div>" +
					"<p title='" + item.cover_infor_user_name +
					"' class='jsx-1904556275 author-avatar_name__3ex3- name line-1'>" +
					"<a href='http://127.0.0.1:8899/html/car_my_homepage.html?" +item.cover_infor_user_id +
					"' title='" + item.cover_infor_user_name + "个人主页 ' target='_blank' rel='opener'>" + item.cover_infor_user_name
					 + "</a></p>" +
					"</div>" +
					"<button onclick=\"insert_or_delete_collection_information('delete',this," + item
					.collection_information_fk_id +
					")\" class='collect-btn tw-hidden tw-absolute tw-z-10 tw-right-0 tw-top-0 tw-h-36 tw-rounded-bl-2 tw-bg-common-black tw-text-white tw-bg-opacity-50 hover:tw-bg-opacity-80 tw-w-80 tw-text-14 tw-rounded-tr-4'>取消收藏</button>" +
					"<button onclick=\"insert_or_delete_collection_information('insert',this," + item
					.collection_information_fk_id +
					")\" class='collect-btn tw-hidden tw-absolute tw-z-10 tw-right-0 tw-top-0 tw-h-36 tw-rounded-bl-2 tw-bg-common-black tw-text-white tw-bg-opacity-50 hover:tw-bg-opacity-80 tw-w-80 tw-text-14 tw-rounded-tr-4' style='display:none'><i class='DCD_Icon icon_collect_24_normal' style='font-size: 18px;'></i></button>" +
					"</div>" +
					"<h3 class='tw-leading-22 tw-text-16 tw-font-medium tw-text-common-black tw-text-justify line-2 g-active-link-text tw-mt-36'>" +
					"<a title='" + item.information.information_title +
					"' href='http://127.0.0.1:8899/html/car_information_details.html?" + item.information
					.information_id + "/0' target='_blank' class='jsx-1095092229' rel='opener'>" +
					item.information.information_title + "</a></h3>" +
					"<p class='tw-mt-8 tw-h-20 tw-lead tw-text-common-gray tw-text-14'>" +
					"<a title='" + item.information.information_title +
					"' href='http://127.0.0.1:8899/html/car_information_details.html?" + item.information
					.information_id +
					"/0' target='_blank' class='jsx-1095092229' rel='opener'><span class='tw-mr-8'>" +
					informationcount + "次阅读</span><span>" +
					collection_informationDate + "</span></a></p>" +
					"</div>" +
					"</div>" +
					"</div>";
			})
			$("#seriesDiv div").remove();
			$("#seriesDiv").append(str);
		}, "JSON");
}

/**
 * 绑定收藏资讯(取消收藏)的点击事件 
 */
function insert_or_delete_collection_information(types, elements, InformationIds) {
	$.ajax({
		url: "http://127.0.0.1:8899/api/consumer/collection_informationController/insert_or_delete_collection_information/",
		type: "post",
		data: JSON.stringify({
			"collection_information": {
				"collection_information_user_fk_id": user_id,
				"collection_information_fk_id": InformationIds
			},
			"type": types
		}),
		contentType: 'application/json;charset=utf-8',
		dataType: "JSON",
		success: function(data) {
			if (data.result == "true") {
				if (types == "insert") {
					$(elements).hide();
					$(elements).prev().show();
				} else if (types == "delete") {
					$(elements).hide();
					$(elements).siblings("button").show();
				}
			}
		}
	});
}

/**
 * 根据登入用户id查询视频收藏集合
 */
function getCollection_VideoList() {
	$.post("http://127.0.0.1:8899/api/consumer/collection_videoController/select_collection_videoList_byId/" +
		user_id,
		function(data) {
			//totalpage=data.totalPage;
			var str = "";
			var collection_videoDate = "";
			var videocount = 0;
			$(data).each(function(index, item) {
				var date = new Date();
				var thisDate = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();

				var newDateCha = parseInt((new Date(thisDate) - new Date(item.video.video_upload_date)) / (
					1000 * 60 * 60 * 24));
				if (newDateCha <= 7) {
					collection_videoDate = newDateCha + "天前";
				} else {
					collection_videoDate = item.video.video_upload_date;
				}
				if (item.video.video_broadcast_count / 10000 >= 1) {
					videocount = (item.video.video_broadcast_count / 10000).toFixed(2) + "万";
				} else {
					videocount = item.video.video_broadcast_count;
				}
				str += "<div>" +
					"<div class='common-card_wrapper__Inr_n '>" +
					"<div>" +
					"<div class='tw-relative tw-text-common-black' style='font-size: 0px;'>" +
					"<a title='" + item.video.video_introduction +
					"' href='http://127.0.0.1:8899/html/car_video_broadcast.html?" + item.video.video_id +
					"' target='_blank' class='jsx-1095092229' rel='opener'>" +
					"<div class='tw-rounded-4 g-load-img-wrap' style='display: block; overflow: hidden; position: relative; z-index: 0;'>" +
					"<div style='display: block; box-sizing: border-box; padding-top: 56%;'></div>" +
					"<img src='http://120.48.47.144:8899/images/" + item.video.video_images_url +
					"' alt='" + item.video.video_introduction +
					"' width='250' height='140' class='zoom' style='position: absolute; inset: 0px; box-sizing: border-box; padding: 0px; border: none; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: cover;'>" +
					"</div>" +
					"<div class='tw-w-50 tw-h-20 tw-flex tw-items-center tw-justify-center tw-rounded-2 tw-bg-black tw-bg-opacity-80 tw-text-white tw-text-12 tw-absolute tw-right-8 tw-bottom-8 tw-z-10'>" +
					"<span class='font-size-10_wrapper__pmvsT undefined' style='width: 44px; height: 10px;'>" +
					"<span class='font-size-10_inner__y3DQ4'><i class='DCD_Icon icon_play_1 tw-text-16 tw-mr-2'></i>18:14</span></span>" +
					"</div>" +
					"</a>" +
					"<div class='jsx-1904556275 author-avatar_avatar-wrap__1pWQN'>" +
					"<div class='jsx-2217341492 avatar_avatar-img__170Bh tw-text-0'>" +
					"<a href='http://127.0.0.1:8899/html/car_my_homepage.html?" + item.cover_user_id +
					"' title='" + item.cover_user_name +
					"个人主页 ' target='_blank' rel='opener'>" +
					"<div class='avatar tw-rounded-full' style='display: inline-block; max-width: 100%; overflow: hidden; position: relative; z-index: 0;'>" +
					"<div style='box-sizing: border-box; display: block; max-width: 100%;'>" +
					"<img alt='' src='data:image/svg+xml;utf-8,<svg width=&quot;40&quot; height=&quot;40&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;/>' aria-hidden='true' role='presentation' style='max-width: 100%; display: block; margin: 0px; border: none; padding: 0px;'>" +
					"</div>" +
					"<img src='http://120.48.47.144:8899/images/" + item.cover_user_head_portrait_url +
					"' alt='" + item.cover_user_name +
					"头像' width='40' height='40' class='' style='position: absolute; inset: 0px; box-sizing: border-box; padding: 0px; border: none; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: cover;'>" +
					"</div><i class='jsx-2217341492 avatar_author-v__2Xocb author-v v-yellow'></i>" +
					"</a>" +
					"</div>" +
					"<p title='" + item.cover_user_name +
					"' class='jsx-1904556275 author-avatar_name__3ex3- name line-1'>" +
					"<a href='http://127.0.0.1:8899/html/car_my_homepage.html?" + item.cover_user_id +
					"' title='" + item.cover_user_name +
					"个人主页 ' target='_blank' rel='opener'>" + item.cover_user_name + "</a></p>" +
					"</div>" +
					"<button onclick=\"insert_or_delete_collection_video('delete',this," + item
					.collection_video_fk_id +
					")\" type='button' class='collect-btn tw-hidden tw-absolute tw-z-10 tw-right-0 tw-top-0 tw-h-36 tw-rounded-bl-2 tw-bg-common-black tw-text-white tw-bg-opacity-50 hover:tw-bg-opacity-80 tw-w-80 tw-text-14 tw-rounded-tr-4' >取消收藏</button>" +
					"<button onclick=\"insert_or_delete_collection_video('insert',this," + item
					.collection_video_fk_id +
					")\" type='button' class='collect-btn tw-hidden tw-absolute tw-z-10 tw-right-0 tw-top-0 tw-h-36 tw-rounded-bl-2 tw-bg-common-black tw-text-white tw-bg-opacity-50 hover:tw-bg-opacity-80 tw-w-80 tw-text-14 tw-rounded-tr-4' style='display:none'><i class='DCD_Icon icon_collect_24_normal' style='font-size: 18px;'></i></button>" +
					"</div>" +
					"<h3 class='tw-leading-22 tw-text-16 tw-font-medium tw-text-common-black tw-text-justify line-2 g-active-link-text tw-mt-36'>" +
					"<a title='" + item.video.video_introduction +
					"' href='http://127.0.0.1:8899/html/car_video_broadcast.html?" + item.video.video_id +
					"' target='_blank' class='jsx-1095092229' rel='opener'>" +
					item.video.video_introduction + "</a></h3>" +
					"<p class='tw-mt-8 tw-h-20 tw-lead tw-text-common-gray tw-text-14'>" +
					"<a title='" + item.video.video_introduction +
					"' href='http://127.0.0.1:8899/html/car_video_broadcast.html?" + item.video.video_id +
					"' target='_blank' class='jsx-1095092229' rel='opener'><span class='tw-mr-8'>" +
					videocount + "万次播放</span><span>" + collection_videoDate +
					"</span></a>" +
					"</p>" +
					"</div>" +
					"</div>" +
					"</div>";
			})
			$("#videoDiv div").remove();
			$("#videoDiv").append(str);
		}, "JSON")
}

/**
 * 绑定收藏视频点击事件
 */
function insert_or_delete_collection_video(types, element, ids) {
	$.ajax({
		url: "http://127.0.0.1:8899/api/consumer/collection_videoController/insert_or_delete_collection_video/",
		type: "post",
		data: JSON.stringify({
			"collection_video": {
				"collection_video_user_fk_id": user_id,
				"collection_video_fk_id": ids
			},
			"type": types
		}),
		contentType: 'application/json;charset=utf-8',
		dataType: "JSON",
		success: function(data) {
			if (data.result == "true") {
				if (types == "insert") {
					$(element).hide();
					$(element).prev().show();
				} else if (types == "delete") {
					$(element).hide();
					$(element).siblings("button").show();
				}
			}
		}
	});
}


/**
 * 切换查看视频收藏和咨询收藏的点击事件
 */
function getClick() {
	//根据用户登入id查询视频收藏集合（先加载展示）
	getCollection_VideoList();
	//先加载页面判断，是否有收藏视频
	existsCollection_Video();
	$("#videoClick").click(function() {
		$(this).addClass("jsx-1262630490 active");
		$("#seriesClick").removeClass("jsx-1262630490 active");
		$("#seriesClick").addClass("jsx-1262630490 ");
		//判断是否收藏视频
		existsCollection_Video();
		//根据用户登入id查询视频收藏集合
		getCollection_VideoList();
		//移除展示咨询的div，方便展示视频div
		$("#seriesDiv div").remove();

	});
	$("#seriesClick").click(function() {
		$(this).addClass("jsx-1262630490 active");
		$("#videoClick").removeClass("jsx-1262630490 active");
		$("#videoClick").addClass("jsx-1262630490 ");
		//判断是否收藏资讯
		existsCollection_Series();
		//根据用户登入id查询咨询收藏集合
		getColltion_informationList();
		//移除展示视频的div，方便展示咨询div
		$("#videoDiv div").remove();
	});
}

/**
 * 判断是否收藏视频
 */
function existsCollection_Video() {
	$.post("http://127.0.0.1:8899/api/consumer/collection_videoController/select_collection_videoList_byId/" +
		user_id,
		function(data) {
			if (data != null && data != '') {
				$(".LookCollection").hide();
				//先隐藏收藏资讯则显示文字“没有更多了”
				$("#SeriesNoMore").hide();
				//如果有收藏视频则显示文字“没有更多了”
				$("#VideoNoMore").show();
			} else {
				$(".LookCollection").show();
				$("#LookSeries").hide();
				$("#LookVideo").show();
				//先隐藏收藏资讯则显示文字“没有更多了”
				$("#SeriesNoMore").hide();
			}
		}, "JSON")
}

/**
 * 判断是否收藏资讯
 */
function existsCollection_Series() {
	$.post("http://127.0.0.1:8899/api/consumer/collection_informationController/select_collection_informationList_byId/" +
		user_id,
		function(data) {
			if (data != null && data != '') {
				$(".LookCollection").hide();
				//先隐藏视频的显示文字“没有更多了“
				$("#VideoNoMore").hide();
				//如果有收藏资讯则显示文字“没有更多了”
				$("#SeriesNoMore").show();
			} else {
				$(".LookCollection").show();
				$("#LookVideo").hide();
				$("#LookSeries").show();
				//先隐藏视频的显示文字“没有更多了“
				$("#VideoNoMore").hide();

			}
		}, "JSON")
}

/**
 * 回到顶部
 */
function GotoTop(windowTop) {
	//向上移动到0px
	$("#gotoTop").click(function() {
		$('html,body').animate({
			scrollTop: '0px'
		}, 'slow');
	});
	//获取最小高度，无法入值则默认10
	windowTop ? windowTop = windowTop : windowTop = 50;

	//为窗口的scroll事件绑定函数
	$(window).scroll(function() {
		//获取当前高度
		var height = $(window).scrollTop();
		if (height > windowTop) {
			$("#gotoTop").fadeIn("slow");
		} else {
			$("#gotoTop").fadeOut("slow");
		}
	});
}
